package com.horizon.cloud.standard.lock;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;

import java.util.Collections;

@Slf4j
@Data
@AllArgsConstructor
public class JedisCommandLock {
    private RedisTemplate redisTemplate;
    private static final String LOCK_SUCCESS = "OK";
    private static final String SET_IF_NOT_EXIST = "NX";
    private static final String SET_WITH_EXPIRE_TIME= "PX";

    public static boolean tryGetDistributedLock(Jedis jedis,
                                                String lockKey,
                                                String requestId,
                                                int expireTime) {
        String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
        if (LOCK_SUCCESS.equals(result)){
            return true;
        }
        return false;
    }

    private static final Long RELEASE_SUCCESS = 1L;

    public static boolean releaseDistributedLock(Jedis jedis,
                                                 String lockKey,
                                                 String requestId) {
        //lua脚本
        String script = "if redis.call('get', KEY[1]) ==ARGV[1] then " +
                "return redis.call('del', KEYS[1]) else return 0 end";
        Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
        if (RELEASE_SUCCESS.equals(result)){
            return true;
        }
        return false;
    }
}
